package com.cxj.musicplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.cxj.musicplayer.R;
import com.cxj.musicplayer.bean.Song;
import com.cxj.musicplayer.component.RoundImageView;
import com.cxj.musicplayer.data.ActivityManager;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.service.MediaService;
import com.cxj.musicplayer.utils.FileUtil;

public class TitleActivity extends Activity implements View.OnClickListener {
    RoundImageView roundImageView;
    private FrameLayout mContentLayout;
    MediaService.MusicController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupViews();
    }

    private void setupViews() {
        super.setContentView(R.layout.activity_title);
        mContentLayout = findViewById(R.id.layout_content);
        roundImageView = findViewById(R.id.little_view);

        roundImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent().setClass(TitleActivity.this, OnPlaying.class);
                Song song = controller.getSongInfo();
                if(song!=null){
                    intent.putExtra("song_id", song.getSong_id());
                    intent.putExtra("song_i", song.getCur_pos());
                    intent.putExtra("song_source", song.getSource());
                }else{
                    return;
                }
                intent.putExtra("from", "reload");
                ActivityManager.pre = TitleActivity.class;
                startActivity(intent);

            }
        });

        controller = (MediaService.MusicController) DataManager.concurrentHashMap_object_data.get("music_controller");
        Song song = controller.getSongInfo();
        if(song!= null){
            roundImageView.setImageBitmap(FileUtil.get_pic_from_local(this, song.getSong_id()));
            if("playing".equals(controller.getPlayState())){
                roundImageView.start_animation();
            }else if("pause".equals(controller.getPlayState())){
                roundImageView.clear_animation();
            }
        }
    }


    protected void showRoundImageView(int roundImageViewResid, boolean show) {
        if (roundImageView != null) {
            if (show) {
                roundImageView.setImageResource(R.drawable.ic_launcher_background);
                roundImageView.setVisibility(View.VISIBLE);
            } else {
                roundImageView.setVisibility(View.INVISIBLE);
            }
        }
    }

    protected void onRoundImageView(View roundImageView) {
        Toast.makeText(this, "点击返回，可在此处调用finish()", Toast.LENGTH_LONG).show();
        //finish();
    }




    //取出FrameLayout并调用父类removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.little_view:
                onRoundImageView(v);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Song song = controller.getSongInfo();
        if(song!= null){
            roundImageView.setImageBitmap(FileUtil.get_pic_from_local(this, song.getSong_id()));
            if("playing".equals(controller.getPlayState())){
                roundImageView.start_animation();
            }else if("pause".equals(controller.getPlayState())){
                roundImageView.clear_animation();
            }
        }
    }
}
